const nATPicMV = require('express').Router()
nATPicMV.route('/wx')
    .post(async (req, res, next) => {
        if (req.jsonData.MsgType[0] == 'image') {
            const { jsonData } = req
            //如果有未完成的NAT检测时收到的文字信息作为房间号使用
            const mongoose = require('mongoose')
            const NAT = mongoose.model('nat', require('../../../model/nATSchema'))
            //console.log('ok')
            const unfinishNAT = await NAT.findOne({ userID: jsonData.FromUserName[0], isFinish: false })
            
            if (unfinishNAT && !unfinishNAT.hasPic) {
                await NAT.updateOne({userID: jsonData.FromUserName[0], isFinish: false},{$set:{
                    pic: jsonData.PicUrl[0],
                    hasPic: true,
                    picID: jsonData.MediaId[0]
                }})
                unfinishNAT.pic = jsonData.PicUrl[0] 
                unfinishNAT.hasPic = true
                //console.log(unfinishNAT)
                if (unfinishNAT.hasAddress && unfinishNAT.hasPic) {
                    //提示用户完成操作
                    const msg = '感谢使用，您的抗原信息已添加完成！'
                    const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], msg)
                    await NAT.updateOne({userID: jsonData.FromUserName[0], isFinish: false},{$set:{
                        isFinish: true
                    }})
                    res.end(xmlSendData)

                } else {
                     //提示用户完成操作
                     const msg = '请输入您所在的楼号和房号'
                     const xmlSendData = require('../../../tools/sendMsg')(jsonData.FromUserName[0], jsonData.ToUserName[0], msg)
                     res.end(xmlSendData)
                }
            } else {
                next()
            }

        } else next()
    })
module.exports = nATPicMV